package com.bjpowernode.web.controller;

import com.bjpowernode.web.Result;
import com.bjpowernode.commons.model.User;
import com.bjpowernode.commons.service.BidInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("bid")
@CrossOrigin
public class BidInfoController {

    @DubboReference
    private BidInfoService bidInfoService;

    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("records")
    public List records(@RequestHeader(value = "token", required = false) String token) {
        User user = (User) redisTemplate.opsForValue().get(token);
        Long userId = user.getId();
        return bidInfoService.getRecords(userId);
    }

    @GetMapping("top3")
    public List top3(HttpServletRequest request) {
        return bidInfoService.top3();
    }

    @PostMapping("bid")
    //接收json格式，参数列表中只能有一个参数用于接收json数据，json数据会被转换成对应的参数类型
    //@ResponseBody声明在方法的上面，用于将方法的返回值转换为json响应客户端，
    //@RequestBody声明在方法的参数列表中，用于将客户端提交的json数据转换成对象
    //public Map bid(@RequestBody Map params) {
    public Map bid(Double money, Long prodId,
                   @RequestHeader(value = "token", required = false) String token) { // 接收非json格式的数据

        User user = (User) redisTemplate.opsForValue().get(token);
        Long userId = user.getId();
        bidInfoService.bid(money, prodId, userId);

        return Result.SUC;
    }

}
